package com.test.event;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

/**
 * @author LuHuanCheng
 * 类说明：事件发布者（触发者）  正常的一个service添加成员变量ApplicationEventPublisher,在正常逻辑处理后添加applicationEventPublisher.publishEvent(new UserRegisterEvent(this, username));
 */
@Service
public class UserService implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    public void registerUser(String username) {
        //1.注册用户正常逻辑
        System.out.println("用户表添加一个用户");

        //2.发布事件
        applicationEventPublisher.publishEvent(new UserRegisterEvent(this, username));
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
}